package com.xiaoyu.client.adapter.seek;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.xiaoyu.client.R;
import com.xiaoyu.client.model.seek.SeekTypeBean.SeekTypeParam;

import java.util.ArrayList;
import java.util.List;

/**
 * 求助类别列表适配器
 */
public class SeekTypeAdapter extends BaseAdapter {
    private Context mContext;
    private List<SeekTypeParam> mList = new ArrayList<>();
    private String selectId;//选中的类别id
    private OnItemTypeClickListener mListener;

    public SeekTypeAdapter(Context mContext, List<SeekTypeParam> mList) {
        this.mContext = mContext;
        this.mList = mList;

    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public SeekTypeParam getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder{
        CheckBox typeTxt;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_seek_type,null);
            holder = new ViewHolder();
            holder.typeTxt = convertView.findViewById(R.id.item_seek_type_txt);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.typeTxt.setText(getItem(position).getSeephelpname());
        if (selectId != null && selectId.equals(getItem(position).getSeephelpid())){
            holder.typeTxt.setChecked(true);
        }else {
            holder.typeTxt.setChecked(false);
        }

        holder.typeTxt.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (mListener != null)
                    mListener.onTypeClick(holder.typeTxt,isChecked,position);
            }
        });
        return convertView;
    }

    public void setSelectId(String selectId) {
        this.selectId = selectId;
        Log.i("jiagnxue","list size = "+mList.size());
        this.notifyDataSetChanged();
    }

    public interface OnItemTypeClickListener{
        void onTypeClick(CheckBox checkBox,boolean isChecked,int position);
    }

    public void setOnItemTypeClickListener(OnItemTypeClickListener mListener) {
        this.mListener = mListener;
    }
}
